clear all
set more off 

cd "C:\Dropbox\Average_target_survey\replication_folder\dofiles"

quiet do load_rreg2

cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step003, clear

***collapse days when the sample size is small
gen start_day2=start_day1
recode start_day2 30/32=29


*** Inflation target of the Fed
tabstat QJH11 [aw=weight], by(start_day1) format("%3.2f") stat(N mean p50 sd iqr)

*** Inflation target of the Fed: inflation is 1%
tabstat QJH123 [aw=weight], by(start_day1) format("%3.2f") stat(N mean p50 sd iqr)

*** Inflation target of the Fed: inflation is 3%
tabstat QJH124 [aw=weight] , by(start_day1) format("%3.2f") stat(N mean p50 sd iqr)

replace QJH123=. if QJH123>=100
replace QJH124=. if QJH124>=100
replace QJH11=. if QJH11>=100

foreach var in  QJH123 QJH124 QJH11 {
	winsor2 `var', cuts(5 95) suffix(_w)
}


*** responses to hypothetical scenarios
gen hypo=QJH123
replace hypo=QJH124 if hypo==.

*** condition on hearing news
gen hypo_news=hypo if QJH3==2

*** condition on picking IT news
gen hypo_anyIT=hypo if QJH10_2==1  | QJH10_1==1 

*** condition on picking AIT
gen hypo_AIT=hypo if QJH10_2==1

*** condition on picking IT news
gen hypo_IT=hypo if  QJH10_1==1 


*** assignmnet into hypothetical scenarios
capture drop treatmentN
gen treatmentN=0 if QJH123~=.
replace treatmentN=1 if QJH124~=.

gen target=QJH11


gen WGT=weight


		
*** compute the difference non-parametrically

tempname 1
postfile `1'  ///
	str20 method ///
	target_center ///
	b_diff ///
	se_diff ///
	N_diff ///
	R2_diff ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table00_AIT_figure_local_regression_LR.dta", replace every(1)
	

gen _hypo=hypo*sqrt(WGT)
gen _treatmentN=treatmentN*sqrt(WGT)
gen _W=sqrt(WGT)

***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** plug-in bandwidth: 0.9*min(std, IQR/1.34)*Nobs^(-1/5)
***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** full sample
sum target [aw=weight], d
di 0.9*min(r(sd),(r(p75)-r(p25))/1.34)/(r(N)^(1/5))


*** subsamples: news
sum target [aw=weight] if QJH3==2, d
di 0.9*min(r(sd),(r(p75)-r(p25))/1.34)/(r(N)^(1/5))
*** subsample:AIT 
sum target [aw=weight] if QJH10_2==1, d
di 0.9*min(r(sd),(r(p75)-r(p25))/1.34)/(r(N)^(1/5))



***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
local bandwidth=2 /* 1.5 */	
local bandwidth2=2 /* 1.5 */	
local bandwidth3=2.5 /* 1.5 */	

quiet forvalues i=0(0.1)10 {
	noisily di "`i'"
	*** OLS regression
	reg hypo treatmentN [aw=WGT]  if abs(target-`i')<=`bandwidth', robust 
	post `1' ("OLS") (`i') (_b[treatmentN]) (_se[treatmentN]) (e(N)) (e(r2))

	capture drop WGTk
	gen WGTk=abs(target-`i')/`bandwidth'
	replace WGTk=3/4*(1-1/5*WGTk^2)/sqrt(5)*(abs(WGTk)<sqrt(5))+0*(abs(WGTk)>sqrt(5)) /*Epanechnikov kernel */
	replace WGTk=WGTk*WGT
	reg hypo treatmentN [aw=WGTk] , robust 
	post `1' ("lpoly") (`i') (_b[treatmentN]) (_se[treatmentN]) (e(N)) (e(r2))

	
	*** double bandwidth for subsamples: heard news
	capture drop WGTk
	gen WGTk=abs(target-`i')/`bandwidth2'
	replace WGTk=3/4*(1-1/5*WGTk^2)/sqrt(5)*(abs(WGTk)<sqrt(5))+0*(abs(WGTk)>sqrt(5)) /*Epanechnikov kernel */
	replace WGTk=WGTk*WGT
	
	*** condition on hearing news
	reg hypo_news treatmentN [aw=WGTk] , robust 
	post `1' ("lpoly news") (`i') (_b[treatmentN]) (_se[treatmentN]) (e(N)) (e(r2))
	
	
	*** double bandwidth for subsamples: heard news
	capture drop WGTk
	gen WGTk=abs(target-`i')/`bandwidth3'
	replace WGTk=3/4*(1-1/5*WGTk^2)/sqrt(5)*(abs(WGTk)<sqrt(5))+0*(abs(WGTk)>sqrt(5)) /*Epanechnikov kernel */
	replace WGTk=WGTk*WGT
	
	*** condition on picking IT
	reg hypo_IT treatmentN [aw=WGTk] , robust 
	post `1' ("lpoly IT") (`i') (_b[treatmentN]) (_se[treatmentN]) (e(N)) (e(r2))
	
	*** condition on picking AIT
	reg hypo_AIT treatmentN [aw=WGTk] , robust 
	post `1' ("lpoly AIT") (`i') (_b[treatmentN]) (_se[treatmentN]) (e(N)) (e(r2))
	
	*** condition on picking any IT
	reg hypo_anyIT treatmentN [aw=WGTk] , robust 
	post `1' ("lpoly anyIT") (`i') (_b[treatmentN]) (_se[treatmentN]) (e(N)) (e(r2))
	
	

	
}	
	
postclose `1'


use "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table00_AIT_figure_local_regression_LR.dta", clear

gen y_90=b_diff+1.96*se_diff
gen y_10=b_diff-1.96*se_diff

gen zero_line=0 
	

capture drop y4
sum y_10
gen y4=r(min) if target_center<=10


twoway ///
	(bar y4 target_center if method=="lpoly" & target_center<=10, fcolor(gs15) lcolor(none)) ///
	(rarea y_90 y_10 target_center if method=="lpoly", fcolor(gs10) lcolor(gs11) ) ///
	(scatter b_diff target_center if method=="lpoly", lcolor(black) lwidth(thick) msymbol(o) mcolor(black) mfcolor(white) connect(direct) ) ///
	///
	(line y_90 target_center if method=="lpoly IT", lpattern(shortdash) lcolor(green)) ///
	(line y_10 target_center if method=="lpoly IT", lpattern(shortdash) lcolor(green)) ///
	(line b_diff target_center if method=="lpoly IT", lcolor(green) lwidth(thick)) ///
	///
	(line y_90 target_center if method=="lpoly AIT", lpattern(longdash) lcolor(blue)) ///
	(line y_10 target_center if method=="lpoly AIT", lpattern(longdash) lcolor(blue)) ///
	(scatter b_diff target_center if method=="lpoly AIT", lcolor(blue) lwidth(thick) msymbol(t) mcolor(blue) mfcolor(white) connect(direct) ) ///
	///
	(line zero_line target_center if method=="lpoly", lcolor(black) lwidth(thin)) ///
	, ///
	legend(order(3 2 6 5 9 8) ///
			label(3 "Full sample") label(2 "90% CI") ///
			label(6 "IT") label(5 "90% CI") ///
			label(9 "Average IT") label(8 "90% CI") ///
			rows(3) position(11) ring(0) rowgap(0.2) colgap(1) size(small) symxsize(10)) 	///			
	ytitle("Difference in response across scenarios, %", size(small))  ///
	xtitle("Fed inflation target, %", size(small)) 	///
	name(fig_response_scenario5x, replace) ///
	title("{bf:Include only those who picked an inflation targeting regime}", size(medium)) ///
	xlabel(0(1)10) ///
	ylabel(-2(1)3) ///
	graphregion(color(white)) bgcolor(white) ///
	yline(0, lcolor(black) lwidth(thin)) ///
	text(-2 1 "AIT") ///
	plotregion(margin(zero))

	
twoway ///
	(bar y4 target_center if method=="lpoly" & target_center<=10, fcolor(gs15) lcolor(none)) ///
	(rarea y_90 y_10 target_center if method=="lpoly", fcolor(gs10) lcolor(gs11) ) ///
	(scatter b_diff target_center if method=="lpoly", lcolor(black) lwidth(thick) msymbol(o) mcolor(black) mfcolor(white) connect(direct) ) ///
	///
	(line y_90 target_center if method=="lpoly IT", lpattern(shortdash) lcolor(black)) ///
	(line y_10 target_center if method=="lpoly IT", lpattern(shortdash) lcolor(black)) ///
	(line b_diff target_center if method=="lpoly IT", lcolor(black) lwidth(thick)) ///
	///
	(line y_90 target_center if method=="lpoly AIT", lpattern(longdash) lcolor(black)) ///
	(line y_10 target_center if method=="lpoly AIT", lpattern(longdash) lcolor(black)) ///
	(scatter b_diff target_center if method=="lpoly AIT", lcolor(black) lwidth(thick) msymbol(t) mcolor(black) mfcolor(white) connect(direct) ) ///
	///
	(line zero_line target_center if method=="lpoly", lcolor(black) lwidth(thin)) ///
	, ///
	legend(order(3 2 6 5 9 8) ///
			label(3 "Full sample") label(2 "90% CI") ///
			label(6 "IT") label(5 "90% CI") ///
			label(9 "Average IT") label(8 "90% CI") ///
			rows(3) position(11) ring(0) rowgap(0.2) colgap(1) size(small) symxsize(10)) 	///			
	ytitle("Difference in response across scenarios, %", size(small))  ///
	xtitle("Fed inflation target, %", size(small)) 	///
	name(fig_response_scenario5x_bw, replace) ///
	title("{bf:Include only those who picked an inflation targeting regime}", size(medium)) ///
	xlabel(0(1)10) ///
	ylabel(-2(1)3) ///
	graphregion(color(white)) bgcolor(white) ///
	yline(0, lcolor(black) lwidth(thin)) ///
	text(-2 1 "AIT") ///
	plotregion(margin(zero))
	
	
	